package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.OaBannerAddDTO;
import com.cheer.dto.OaBannerUpdateDTO;
import com.cheer.service.OaBannerService;
import com.cheer.vo.OaBannerInfoVO;
import com.cheer.vo.OaBannerListVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/banner")
public class OaBannerController {

    @Autowired
    private OaBannerService oaBannerService;

    @ApiOperation("添加轮播图")
    @PostMapping("/add")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result add(@RequestBody @Valid OaBannerAddDTO bannerAddDTO) {
        oaBannerService.add(bannerAddDTO);
        return Result.success();
    }

    @ApiOperation("查询轮播图列表")
    @GetMapping("/list")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<Page<OaBannerListVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(oaBannerService.list(pageNum, pageSize));
    }

    @ApiOperation("批量删除")
    @PostMapping("/delete")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result delete(@RequestBody List<Long> ids) {
        oaBannerService.delete(ids);
        return Result.success();
    }

    @ApiOperation("查询轮播图详情")
    @GetMapping("/info")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<OaBannerInfoVO> info(@RequestParam Integer id) {
        return Result.success(oaBannerService.info(id));
    }

    @ApiOperation("修改轮播图")
    @PostMapping("/update")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result update(@RequestBody @Valid OaBannerUpdateDTO bannerUpdateDTO) {
        oaBannerService.update(bannerUpdateDTO);
        return Result.success();
    }

    @ApiOperation("启用或禁用")
    @GetMapping("/status")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result updateStatus(@RequestParam Integer id, @RequestParam Integer status) {
        oaBannerService.updateStatus(id, status);
        return Result.success();
    }

}
